/**
 * @author yxw
 * date : 2013年9月30日 下午3:49:42 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.view.ImageBitmapAdapter;
import com.tzinfo.tzt.domain.SeeNote;
import com.tzinfo.tzt.service.HouseSeeNoteService;
import com.tzinfo.tzt.util.StringTools;

public class SeeNoteListActivity extends Activity implements OnClickListener,
		OnItemClickListener, OnItemLongClickListener {

	private ImageButton addListBtn;
	private ListView houseList;
	private ImageBitmapAdapter bitMapAdapter;
	private List<Map<String, Object>> data;
	private List<SeeNote> seeNoteList;
	private HouseSeeNoteService seeNoteService;

	protected SeeNote seeNote;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_house);
		seeNoteService = new HouseSeeNoteService(this);
		new Thread(initRun).start();
	}

	private void init() {
		addListBtn = (ImageButton) this.findViewById(R.id.bt_add_list);

		houseList = (ListView) this
				.findViewById(R.id.listview_looking_house_list);
		getData();
		
		bitMapAdapter = new ImageBitmapAdapter(this, data,
				R.layout.item_looking_house_list, new String[] { "name",
						"address", "info", "time", "image1", "image2",
						"image3", "image4" }, new int[] { R.id.text_house_name,
						R.id.text_address, R.id.text_house_info,
						R.id.text_time, R.id.images_1, R.id.images_2,
						R.id.images_3, R.id.images_4 });
		houseList.setAdapter(bitMapAdapter);
		houseList.setOnItemClickListener(this);
		houseList.setOnItemLongClickListener(this);
		addListBtn.setOnClickListener(this);
	}

	private void getData() {
		seeNoteList = seeNoteService.getSeeNoteList();
		if (data == null) {
			data = new ArrayList<Map<String, Object>>();
		} else {
			data.clear();
		}

		for (int i = 0; i < seeNoteList.size(); i++) {
			Map<String, Object> map;
			SeeNote note = seeNoteList.get(i);
			map = new HashMap<String, Object>();
			map.put("name", note.getVillageName());
			map.put("address", note.getVillageAddress());
			String info = "";
			if (!StringTools.isEmpty(note.getType())) {
				info += note.getType();
			}
			if (!"0".equals(note.getSize() + "")) {
				info += " " + note.getSize() + "平米";
			}
			if (!StringTools.isEmpty(note.getPrice())) {
				info += " " + note.getPrice() + "万";
			}
			map.put("info", info);
			map.put("time", note.getLastModifyTime());
			List<String> list = note.getImages();
			for (int j = 0; j < 4; j++) {
				map.put("image" + (j + 1), R.drawable.point_null);
			}
			for (int j = 0; list!= null&&list.size()>0
					&& j < (list.size() <= 4 ? list.size() : 4); j++) {
				Log.e("i="+i, list.get(j));
				map.put("image" + (j + 1), list.get(j));
			}
			data.add(map);
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(bitMapAdapter!=null){
			getData();
			bitMapAdapter.notifyDataSetChanged();
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_add_list:
			// 跳转到小区搜索界面
			Intent intent = new Intent(SeeNoteListActivity.this,
					SeeNotesUpdateActivity.class);
			// Intent intent=new Intent(ListHouseActivity.this,
			// ApartmentSearchActivity.class);
			startActivity(intent);
			// Toast.makeText(ListHouseActivity.this, "该模块尚未开通，敬请期待~！",
			// 5000).show();
			// 测试

			break;

		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(SeeNoteListActivity.this,
				SeeNotesDetailActivity.class);
		seeNote = seeNoteList.get((int) arg3);
		intent.putExtra("seeNote", seeNote);
		Log.e("seeNote", seeNote.toString());
		startActivity(intent);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		seeNote = seeNoteList.get((int) arg3);
		new AlertDialog.Builder(SeeNoteListActivity.this)
				.setTitle(getResources().getString(R.string.tips))
				.setMessage("您确定要删除该笔记吗？")
				.setPositiveButton("取	消",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						})
				.setNegativeButton("确	定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								seeNoteService.deleteSeeNoteById(seeNote
										.getId() + "");
								getData();
								bitMapAdapter.notifyDataSetChanged();
							}
						}).show();
		return false;
	}
	
	
	Runnable initRun=new Runnable() {
		
		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			msg.arg1=1;
			handler.sendMessage(msg);
			
		}
	};
		

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				init();
				break;
			}
		}
	};
}
